package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.EventToEvaluateUser;
import com.sl.au.entity.ViewForUserScoreAnalysis;
import com.sl.au.service.EventToEvaluateUserService;
import com.sl.au.service.ViewForUserScoreAnalysisService;
import com.sl.utils.ExdeUtil;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping(value = "/ui/ScoreAnalysis")
public class ScoreAnalysisController extends BaseController {

	@Autowired
	ViewForUserScoreAnalysisService viewForUserScoreAnalysisService;
	
	@Autowired
	EventToEvaluateUserService eventToEvaluateUserService;
	

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("analysis/scoreanalysisindex");
		return modelAndView;
	}

	@RequestMapping(value = "getUserPage", method = RequestMethod.POST)
	public Map<String, Object> getUserPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<ViewForUserScoreAnalysis> users = null;
		int count = 0;
		users = viewForUserScoreAnalysisService.getPage(start, length, key, null, null);
		count = viewForUserScoreAnalysisService.getCount(key);
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getEvaluatePage", method = RequestMethod.POST)
	public Map<String, Object> getEvaluatePage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		
		
		String dataType = request.getParameter("dataType");
		String dataId = request.getParameter("dataId");
		List<EventToEvaluateUser> pages = null;
		int count = 0;
		if(dataType.equals("")) {
			pages = new ArrayList<EventToEvaluateUser>();
		}else {
			
			String cyc = request.getParameter("cyc");
			String begin = request.getParameter("beginDate");
			String end = request.getParameter("endDate");
			String uId = request.getParameter("uId");
			Date beginDate = null;
			Date endDate = null;
			if(cyc.equals("week")) {
				beginDate = ExdeUtil.getThisWeekBegin();
				endDate = ExdeUtil.getThisWeekEnd();
			}else if(cyc.equals("month")) {
				beginDate = ExdeUtil.getThisMonthBegin();
				endDate = ExdeUtil.getThisMonthEnd();
			}else if(cyc.equals("year")) {
				beginDate = ExdeUtil.getThisYearBegin();
				endDate = ExdeUtil.getThisYearEnd();
			}else {
				String pattern = "yyyy-MM-dd HH:mm:ss";
				begin += " 00:00:00";
				end += " 23:59:59";
				beginDate = ExdeUtil.str2Date(begin, pattern);
				endDate = ExdeUtil.str2Date(end, pattern);
				
			}
			
			pages = eventToEvaluateUserService.getPageByDate(start, length, uId, key, beginDate, endDate, dataType, dataId, null, null);
			count = eventToEvaluateUserService.getCountByDate(uId, key, beginDate, endDate,  dataType, dataId);
		}
		
		map.put("data", pages);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping("/getAnalysisData/{id}")
	public Map<String, Object> getAnalysisData(@PathVariable("id") String id,@RequestBody Map<String,Object> map) {
		String cyc = (String) map.get("cyc");
		String begin = (String) map.get("beginDate");
		String end = (String) map.get("endDate");
		Date beginDate = null;
		Date endDate = null;
		if(cyc.equals("week")) {
			beginDate = ExdeUtil.getThisWeekBegin();
			endDate = ExdeUtil.getThisWeekEnd();
		}else if(cyc.equals("month")) {
			beginDate = ExdeUtil.getThisMonthBegin();
			endDate = ExdeUtil.getThisMonthEnd();
		}else if(cyc.equals("year")) {
			beginDate = ExdeUtil.getThisYearBegin();
			endDate = ExdeUtil.getThisYearEnd();
		}else {
			String pattern = "yyyy-MM-dd HH:mm:ss";
			begin += " 00:00:00";
			end += " 23:59:59";
			beginDate = ExdeUtil.str2Date(begin, pattern);
			endDate = ExdeUtil.str2Date(end, pattern);
			
		}
		Map<String, Object> data  = viewForUserScoreAnalysisService.getAnalysisData(beginDate, endDate, id);
		data.put("code", 1);
		return data;
	}

}
